package com.example.activity;

import java.util.List;

import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.R;
import com.example.entity.ViewTag;
import com.example.module.linker.KakaoLink;
import com.example.persistence.ImageActivity;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.assist.SimpleImageLoadingListener;

public class CaptureGridActivity extends ImageActivity {

    private GridView gridView;
	private List<String> captureList;
	private DisplayImageOptions options;

	@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_capture);
        
        gridView = (GridView)findViewById(R.id.gridView);
        captureList = getCaptureList();
        
        options = new DisplayImageOptions.Builder()
			.showStubImage(R.drawable.stub_image)
			.showImageForEmptyUri(R.drawable.image_for_empty_url)
			.cacheInMemory()
			.cacheOnDisc()
			.build();
        
		gridView.setAdapter(new ImageAdapter());
		gridView.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
				if (isSelectable)
					toggleView(position, view);
				else
					startImageGalleryActivity(position);
			}
		});
    }
	
	private void startImageGalleryActivity(int position) {
		Intent intent = new Intent(this, CaptureDetailActivity.class);
		intent.putExtra(IMAGE_POSITION, position);
//		startActivity(intent);
		switchActivity(intent);
	}
	
	public class ImageAdapter extends BaseAdapter {
		@Override
		public int getCount() {
			return captureList.size();
		}

		@Override
		public Object getItem(int position) {
			return null;
		}

		@Override
		public long getItemId(int position) {
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			View imageLayout = convertView;
			if (convertView == null)
				imageLayout = getLayoutInflater().inflate(R.layout.item_grid, parent, false);
			
			String fileName = captureList.get(position);
			final ImageView imageView = (ImageView) imageLayout.findViewById(R.id.image);
			final TextView textView = (TextView) imageLayout.findViewById(R.id.text);
			textView.setText(getTimestampFromFileName(fileName));
			
			imageLoader.displayImage(fileName, imageView, options, new SimpleImageLoadingListener() {
				@Override
				public void onLoadingComplete(Bitmap loadedImage) {
					Animation anim = AnimationUtils.loadAnimation(CaptureGridActivity.this, R.anim.fade_in);
					imageView.setAnimation(anim);
					anim.start();
				}
			});

			return imageLayout;
		}
	}
	
	@Override
	protected void doShare(List<View> selectedViews) {
		if (selectedViews == null || selectedViews.size() < 1)
			return;
		
		KakaoLink kakaoLink = KakaoLink.getLink(getApplicationContext());
		if (!kakaoLink.isAvailableIntent()) {
			alert("Not installed KakaoTalk.");		
			return;
		}
		
		View view =selectedViews.get(0);
		ViewTag viewTag = (ViewTag) view.getTag();
		
		Intent kakaoIntent = kakaoLink.getIntentForSendImage(captureList.get(viewTag.getPosition()));
		startActivityForResult(kakaoIntent, KAKAO_TALK_CODE);
	}

	@Override
	protected void doDelete(List<View> selectedViews) {
		
	}
}
